package com.example.lynandroid.activity

import android.app.Activity
import android.app.Application
import android.content.*
import android.net.Uri
import android.os.Bundle
import android.os.IBinder
import androidx.fragment.app.FragmentActivity
import com.example.lynandroid.App
import com.example.lynandroid.databinding.BootBinding
import com.example.lynandroid.enums.GoAppType
import com.example.lynandroid.utils.getActivityName
import com.example.lynandroid.utils.getAppName
import com.example.lynandroid.utils.getServiceName
import com.example.lynandroidbeta.IMyAidlInterface

class BootActivity : FragmentActivity() {
    var appName = "com.example.lynandroidbeta"
    var mainActivityName = "com.example.lynandroidbeta.MainActivity"
    private var myAidl: IMyAidlInterface? = null

    lateinit var binding: BootBinding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = BootBinding.inflate(layoutInflater).apply {
            setContentView(root)
            callback = object : CallBack {
                override fun goActivity(type: GoAppType) {
                    appName = getAppName(type)

                    mainActivityName = getActivityName(type)
                    val context: Context = App.context!!
                    try {
                        val application = context as Application
                        val applicationClass = Application::class.java

                        val mBaseMethod = applicationClass.getMethod("getBaseContext")
                        mBaseMethod.isAccessible = true
                        val mBaseObject = mBaseMethod.invoke(application)

                        val mLoadedApkField = applicationClass.getDeclaredField("mLoadedApk")
                        mLoadedApkField.isAccessible = true
                        val mLoadedApkObject = mLoadedApkField[application]
                        val mPackageNameFiled =
                            mLoadedApkObject.javaClass.getDeclaredField("mPackageName")
                        mPackageNameFiled.isAccessible = true
                        mPackageNameFiled[mLoadedApkObject] = "com.ifreetalk.ftalk"
                        val mContentResolverField =
                            mBaseObject.javaClass.getDeclaredField("mContentResolver")
                        mContentResolverField.isAccessible = true
                        val mContentResolverFieldObject =
                            mContentResolverField[mBaseObject] as ContentResolver
                        val mContentPackageNameFiled =
                            ContentResolver::class.java.getDeclaredField("mPackageName")
                        mContentPackageNameFiled.isAccessible = true
                        mContentPackageNameFiled[mContentResolverFieldObject] =
                            "com.ifreetalk.ftalk"

                        val mBasePackageNameField =
                            mBaseObject.javaClass.getDeclaredField("mBasePackageName")
                        mBasePackageNameField.isAccessible = true
                        mBasePackageNameField[mBaseObject] = "com.ifreetalk.ftalk"

                        val activityClass = Activity::javaClass
                        val mReferrerField = activityClass.javaClass.getDeclaredField("mReferrer")
                        mReferrerField[this] = "hhhhhhhhhhhhhhhhhhhhhhhhh"
                    } catch (e: java.lang.Exception) {
                        e.printStackTrace()
                    }

                    this@BootActivity.goActivity()
                }

                override fun goService(type: GoAppType) {
                    appName = getAppName(type)
                    mainActivityName = getServiceName(type)
                    bindAidl()
                    myAidl?.fight("加油红天使")
                }

                override fun goBroadCast(type: GoAppType) {
                    TODO("Not yet implemented")
                }
            }
            webview.loadUrl("http://192.168.101.41/test.html")
        }
    }

    private fun bindAidl() {
        Intent().apply {
            component = ComponentName(appName, mainActivityName)
        }.also {
            bindService(it, object : ServiceConnection {
                override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                    myAidl = IMyAidlInterface.Stub.asInterface(service)
                }

                override fun onServiceDisconnected(name: ComponentName?) {

                }
            }, Context.BIND_AUTO_CREATE)
        }
    }


    private fun goActivity() {
        try {
            goActivity2()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    // 通过包名启动应用,在Android11以上设备需要在清单文件配置软件包可见
    private fun goActivity1() {
        startActivity(Intent(packageManager.getLaunchIntentForPackage(appName)))
    }

    private fun goActivity2() {
        Intent().apply {
            component = ComponentName(appName, mainActivityName)
            //setClassName(appName, mainActivityName)
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
        }.also {
            startActivity(it)
        }
    }

    private fun goActivity3() {
        Intent().apply {
            data = Uri.parse("ftalkschema://123456")
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
        }.also { startActivity(it) }
    }


    override fun onProvideReferrer(): Uri {
        return Uri.parse("假的APP")
    }


    interface CallBack {
        fun goActivity(type: GoAppType)
        fun goService(type: GoAppType)
        fun goBroadCast(type: GoAppType)
    }

}
